package com.zzs.team.DAO;

import com.zzs.team.Entity.Team;
import com.zzs.team.Entity.TeamShow;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface TeamDao {

    @Insert("insert into teams(createTime,schoolName,typeName,reservation,userID,AllNumber,hasNumber,title,QQorWechat,QQorWechatText,aim,teamInfo,Request)" +
            "values(#{createTime},#{schoolName},#{typeName},#{reservation},#{userID},#{AllNumber},#{hasNumber},#{title},#{qqorWechat},#{qqorWechatText},#{aim},#{teamInfo},#{Request})")
    void InsertTeam(Team team);

    @Select("select * from teams ORDER BY createTime DESC ")
    @Results(id ="teamShowMap",value = {
            @Result(id=true,column = "teamID",property = "teamID"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "schoolName",property = "schoolName"),
            @Result(column = "typeName",property = "typeName"),
            @Result(column = "reservation",property = "reservation"),
            @Result(column = "userID",property = "userID"),
            @Result(column = "AllNumber",property = "AllNumber"),
            @Result(column = "hasNumber",property = "hasNumber"),
            @Result(column = "title",property = "title"),
            @Result(column = "QQorWechat",property = "qqorWechat"),
            @Result(column = "QQorWechatText",property = "qqorWechatText"),
            @Result(column = "aim",property = "aim"),
            @Result(column = "teamInfo",property = "teamInfo"),
            @Result(column = "Request",property = "Request"),
            @Result(column = "userID",property = "user",one = @One(select = "com.zzs.team.DAO.UserDao.findUserByOpenid"))
    })
    List<TeamShow> getTeams();

    @Select("select * from teams where teamID = #{teamid}")
    @ResultMap(value = {"teamShowMap"})
    List<TeamShow> getTeamById(Integer teamid);

    @Select("select * from teams where schoolName in (select schoolName from school_info where cityID in (select cityID from city_info where provinceID = (select provinceID from province_info where provinceName = #{provinceName})) ) ORDER BY createTime DESC")
    @ResultMap(value = {"teamShowMap"})
    List<TeamShow> getTeamsByProvinceName(String provinceName);

    @Select("select * from teams where schoolName in ( select schoolName from school_info where cityID = (select cityID from city_info where cityName = #{cityName}) )")
    @ResultMap(value = {"teamShowMap"})
    List<TeamShow> getTeamsByCityName(String cityName);

    @Select("select * from teams where schoolName = #{schoolName}")
    @ResultMap(value = "teamShowMap")
    List<TeamShow> getTeamBySchoolName(String schoolName);

    @Select("select * from teams where userID = #{userID} ORDER BY createTime DESC")
    @Results(value = {
            @Result(id=true,column = "teamID",property = "teamID"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "schoolName",property = "schoolName"),
            @Result(column = "typeName",property = "typeName"),
            @Result(column = "reservation",property = "reservation"),
            @Result(column = "AllNumber",property = "AllNumber"),
            @Result(column = "hasNumber",property = "hasNumber"),
            @Result(column = "title",property = "title"),
            @Result(column = "QQorWechat",property = "qqorWechat"),
            @Result(column = "QQorWechatText",property = "qqorWechatText"),
            @Result(column = "aim",property = "aim"),
            @Result(column = "teamInfo",property = "teamInfo"),
            @Result(column = "Request",property = "Request"),
            @Result(column = "userID",property = "user",one = @One(select = "com.zzs.team.DAO.UserDao.findUserByOpenid"))
    })
    List<TeamShow> getMyTeams(String userID);

    @Delete("delete from teams where userID=#{userID} and teamID=#{teamID}")
    void deleteTeamByTeamid(String userID,String teamID);

    @Update("update teams set schoolName=#{t.schoolName},typeName=#{t.typeName},reservation=#{t.reservation},AllNumber=#{t.AllNumber}," +
            "hasNumber=#{t.hasNumber},title=#{t.title},QQorWechat=#{t.qqorWechat},QQorWechatText=#{t.qqorWechatText}," +
            "aim=#{t.aim},teamInfo=#{t.teamInfo},Request=#{t.Request}  where userID=#{uID} and teamID = #{t.teamID}")
    void updateTeam(@Param("uID") String uID,@Param("t") Team t);

    @Update("update teams set hasNumber = #{newhasNumber} where teamID = #{teamID}")
    void updateTeamhasNumber(Integer teamID,Integer newhasNumber);
}
